package top.maof.book.spider.develop.contents.bqg;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import top.maof.book.model.Book;
import top.maof.book.model.Contents;
import top.maof.book.spider.develop.Split;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.HelpUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;

@Data
@TargetUrl("https://www.biqusa.com/\\d+_\\d+/")
@HelpUrl(value = {"https://www.biqusa.com/xiuzhenxiaoshuo/", "https://www.biqusa.com/xuanhuanxiaoshuo/",
        "https://www.biqusa.com/dushixiaoshuo/", "https://www.biqusa.com/chuanyuexiaoshuo/",
        "https://www.biqusa.com/wangyouxiaoshuo/",
        "https://www.biqusa.com/kehuanxiaoshuo/", "https://www.biqusa.com/qitaxiaoshuo/",
        "https://www.biqusa.com/wanben/\\d+_\\d+/",
        "https://www.biqusa.com/wangyouxiaoshuo/\\d+_\\d+.html", "https://www.biqusa.com/xiuzhenxiaoshuo/\\d+_\\d+.html",
        "https://www.biqusa.com/xuanhuanxiaoshuo/\\d+_\\d+.html", "https://www.biqusa.com/dushixiaoshuo/\\d+_\\d+.html",
        "https://www.biqusa.com/chuanyuexiaoshuo/\\d+_\\d+.html",
        "https://www.biqusa.com/kehuanxiaoshuo/\\d+_\\d+.html", "https://www.biqusa.com/qitaxiaoshuo/\\d+_\\d+.html",
        "https://www.biqusa.com/xiaoshuodaquan/3.html", "https://www.biqusa.com/xiaoshuodaquan/1.html",
        "https://www.biqusa.com/xiaoshuodaquan/2.html", "https://www.biqusa.com/xiaoshuodaquan/4.html",
        "https://www.biqusa.com/xiaoshuodaquan/5.html", "https://www.biqusa.com/xiaoshuodaquan/6.html",
        "https://www.biqusa.com/xiaoshuodaquan/7.html"})
@Slf4j
public class Biqusa implements AfterExtractor, Split {

    @ExtractBy(value = "//div[@id=info]/h1/text()")
    private String name;

    @ExtractBy(value = "//div[@id=info]/p[1]/a/text()")
    private String author;//作者:伊然淳静

    private String url;

    private String content;

    private Integer webId = 12;

    public static final String CHARSET = "utf-8";

    @Override
    public Book getBook() {
        Book book = new Book();
        book.setAuthor(this.author);
        book.setName(this.name);
        return book;
    }

    @Override
    public Contents getContents() {
        Contents contents = new Contents();
        contents.setUrl(this.url);
        contents.setContent(this.content);
        contents.setWebId(this.webId);
        return contents;
    }


    @Override
    public void afterProcess(Page page) {

        //对得到的作者字段做处理  作者：唐家三少

        log.info(this.name + "-" + this.author);


        //获取该页面url
        String url = page.getRequest().getUrl();
        this.setUrl(url);
    }
}
